package com.gz.mylife.app.keys;

import java.lang.ref.WeakReference;

import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.gz.mylife.App;
import com.gz.mylife.R;
import com.gz.mylife.app.BaseActivity;
import com.gz.mylife.http.AsyncHttpClient;
import com.gz.mylife.http.AsyncHttpResponseHandler;
import com.gz.mylife.http.RequestParams;
import com.zl.smartmall.library.po.LoginData;
import com.zl.smartmall.library.util.Constants;
/**
 * 门禁系统忘记密码重新设置密码
 * @author Administrator
 *
 */
public class FindActivity extends BaseActivity implements OnClickListener {

	private String phone;
	private String register_password;
	private String register_password2;
	private String password;
	private String phone_sn;
	private EditText etNewNum;
	private EditText etFindPwd;
	private EditText etFindPwd2;
	private EditText etSms;
	private MyHandler mHandler;
	private int time = 59;
	private TextView mTvResend;//发送验证码倒计时
	private Dialog mLoadingData;
	private Button btn_update;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find);
		setActionbarTitle("密码找回", false);
		mLoadingData = new Dialog(this, R.style.DialogTheme);
		View loadView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.loading_data_dialog2, null);
		mLoadingData.setContentView(loadView);
		mLoadingData.setCanceledOnTouchOutside(true);
		etNewNum = (EditText) findViewById(R.id.et_new_num);
		etFindPwd = (EditText) findViewById(R.id.et_find_pwd);
		etFindPwd2 = (EditText) findViewById(R.id.et_find_pwd2);
		etSms = (EditText) findViewById(R.id.et_sms);
		btn_update = (Button) findViewById(R.id.btn_update);
		btn_update.setText("提交");
		btn_update.setOnClickListener(this);
		mTvResend = (TextView) findViewById(R.id.tv_resend);
		mTvResend.setTextColor(getResources().getColor(R.color.global_orange));//倒计时
		mTvResend.setClickable(true);
		mTvResend.setOnClickListener(this);
		mHandler = new MyHandler(this);
		TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
		phone_sn = TelephonyMgr.getDeviceId();

	}

	private Runnable leftTime = new Runnable() {

		@Override
		public void run() {
			if(time>0){
				mTvResend.setEnabled(false);
				mTvResend.setTextColor(getResources().getColor(R.color.divid_item_gray));
				mTvResend.setText("("+time+")后重新获取");
				mHandler.postDelayed(leftTime, 1000);
				time--;
			}else{
				time = 59;
				mTvResend.setEnabled(true);
				mTvResend.setTextColor(getResources().getColor(R.color.global_orange));
				mTvResend.setText("重新获取");
				mHandler.removeCallbacks(leftTime);
			}

		}
	};
	private static class MyHandler extends Handler {
		private FindActivity mOuter;

		public MyHandler(FindActivity outer) {
			this.mOuter = outer;
		}

		public void release() {
			new WeakReference<FindActivity>(mOuter);
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				break;

			default:
				break;
			}
		}
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.btn_update:
			phone = etNewNum.getText().toString().trim();
			register_password = etFindPwd.getText().toString().trim();
			register_password2 = etFindPwd2.getText().toString().trim();
			password = etSms.getText().toString().trim();
			if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(register_password)|| TextUtils.isEmpty(register_password2)|| TextUtils.isEmpty(password)) {
				if (TextUtils.isEmpty(phone)) {

					Toast.makeText(this, "请输入手机号！！！", 0).show();
					return;
				}
				if (TextUtils.isEmpty(register_password)) {
					Toast.makeText(this, "请输入新密码！！！", 0).show();
					return;
				}
				if (TextUtils.isEmpty(register_password2)) {
					Toast.makeText(this, "请再次输入新密码！！！", 0).show();
					return;
				}
				if (TextUtils.isEmpty(password)) {

					Toast.makeText(this, "请输入验证码！！！", 0).show();
					return;
				}
			}
			if (register_password.length()<6) {
				Toast.makeText(this, "密码位数不少于6位", Toast.LENGTH_SHORT).show();
				break;
			}
			if (!register_password.equals(register_password2)) {
				Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
				break;
			}
			String find_url = Constants.UPDATE_URL;//门禁--找回密码
			AsyncHttpClient httpClient_find=new AsyncHttpClient();
			RequestParams params_find=new RequestParams();
			params_find.put("phone", phone);
			params_find.put("register_password", register_password);
			params_find.put("password", password);
			params_find.put("phone_sn", phone_sn);
			//这个是get方法请求，自己调配，2者写一就行
			httpClient_find.get(find_url ,params_find,new AsyncHttpResponseHandler(){

				@Override
				public void onSuccess(String content) {
					super.onSuccess(content);
					Gson gson = new Gson();
					LoginData mLoginData = gson.fromJson(content, LoginData.class);
					//验证成功
					if(mLoginData.ref==0){
						Intent i = new Intent(FindActivity.this,LoginDoorActivity.class);
						FindActivity.this.startActivity(i);
						finish();
					}else {

						Toast.makeText(FindActivity.this,mLoginData.msg, 0).show();
					}
				}
				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);

					Toast.makeText(FindActivity.this,"请求失败，请检查网络。", 0).show();
				}
			});
			//点击提交按钮（调用重置密码的接口）
			break;
		case R.id.tv_resend://点击获取手机验证码按钮
			phone = etNewNum.getText().toString().trim();
			if (TextUtils.isEmpty(phone)) {
				App.mTimeState = false;
				Toast.makeText(this, "请输入手机号", 0).show();
				return;
			}else{
				mTvResend.setText("("+time+")后重新获取");
				mHandler.postDelayed(leftTime, 1000);
			}
			App.mTimeState = true;
			String url = Constants.SMS_URL;//请求验证码
			AsyncHttpClient httpClient=new AsyncHttpClient();
			RequestParams params=new RequestParams();
			params.put("phone", phone);
			//这个是get方法请求，自己调配，2者写一就行
			httpClient.get(url ,params,new AsyncHttpResponseHandler(){

				@Override
				public void onSuccess(String content) {
					super.onSuccess(content);
					Gson gson = new Gson();
					LoginData mLoginData = gson.fromJson(content, LoginData.class);
					//验证成功
					if(mLoginData.ref==0){
					}else {
						Toast.makeText(FindActivity.this,mLoginData.msg, 0).show();
					}
				}
				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);
					Toast.makeText(FindActivity.this,"验证码发送失败，请检查 网络。", 0).show();
				}
			});
			break;

		}

	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mHandler.removeCallbacks(leftTime);
		mHandler.release();
	}
}
